package com.agrm.order.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.rocketmq.spring.support.RocketMQMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.MessageConverter;

/**
 * @Author 22:30Plane
 * @Version 1.0.0
 * <p>
 * created by 22:30Plane on 2025/9/17, last modified by 22:30Plane on 2025/9/17
 */
@Configuration
public class MQConfig {
    //自定义消息序列化器，以支持jdk8日期类型
    @Bean
    @Primary
    public RocketMQMessageConverter customizeRocketMQMessageConverter() {
        RocketMQMessageConverter converter = new RocketMQMessageConverter();
        //获取内部的CompositeMessageConverter并遍历找到MappingJackson2MessageConverter
        CompositeMessageConverter compositeConverter = (CompositeMessageConverter) converter.getMessageConverter();

        for (MessageConverter messageConverter : compositeConverter.getConverters()) {
            if (messageConverter instanceof MappingJackson2MessageConverter jackson2MessageConverter) {
                ObjectMapper objectMapper = jackson2MessageConverter.getObjectMapper();
                // 注册 JavaTimeModule 以支持 LocalDateTime 等 Java 8 时间类型[1](@ref)
                objectMapper.registerModule(new JavaTimeModule());
                //禁用将日期序列化为时间戳，使其输出更可读的字符串格式（如 "2025-09-02T22:36:19"）
                objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            }
        }
        return converter;
    }

}
